package com.ebelter.bodyfatscale.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.ebelter.bodyfatscale.ApplicationHelper;
import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.moudules.https.NetUtils;
import com.ebelter.bodyfatscale.ui.view.dialogs.LoadingDialog;
import com.ebelter.bodyfatscale.util.ActivityManagers;
import com.ebelter.bodyfatscale.util.StatusBarUtil;
import com.ebelter.bodyfatscale.util.StringUtils;
import com.ebelter.btlibrary.util.ULog;

import java.util.List;

import butterknife.ButterKnife;



public abstract class BaseActivity extends AppCompatActivity {

    private static final String TAG = "BaseActivity";
    protected boolean isActivityResume;
    public static String unitKg = StringUtils.getResStr(R.string.kg);
    public static String unitLb = StringUtils.getResStr(R.string.lb);
//      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
//    <uses-permission android:name="android.permission.INTERNET" />
//      <uses-permission android:name="android.permission.BLUETOOTH" />
//    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

    private static final String[] permissions=new String[]{
            Manifest.permission.BLUETOOTH,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.INTERNET,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.REQUEST_INSTALL_PACKAGES,
            Manifest.permission.BLUETOOTH_ADMIN};

   // private static final String[] permissions = new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE};

    public Animation shake;

    private LoadingDialog loadingDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ULog.i(TAG, "-----onCreate----" + this.getLocalClassName());
        //ActivityCompat.requestPermissions(this, permissions, 1);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);


        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        setContentView(loadLayoutById());
        checkPermissions();

        shake = AnimationUtils.loadAnimation(this, R.anim.shake);//加载动画资源文件
        StatusBarUtil.setTranslucent(this, 0);
        StatusBarUtil.setDarkStatusIcon(this,true);


        ButterKnife.bind(this);

        //其他view的初始化
        initViews();
        //view数据初始化
        initData();
        
    }

    public void showLoadingDialog(String tip){
        if (loadingDialog==null){
            loadingDialog=new LoadingDialog(this);
        }
        if (!loadingDialog.isShowing()){
            loadingDialog.show();
        }
        loadingDialog.setTip(tip);

    }

    public void dissLoadingDialog(){
        if (loadingDialog!=null&&loadingDialog.isShowing()){
            loadingDialog.cancel();
        }
    }

    protected abstract int loadLayoutById() ;

    protected abstract void initViews();

    protected abstract void initData();



    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

    @Override
    protected void onStart() {
        ULog.i(TAG, "-----onStart----" + this.getLocalClassName());
        super.onStart();
    }

    @Override
    protected void onResume() {
        ULog.i(TAG, "-----onResume----" + this.getLocalClassName());
        super.onResume();
        isActivityResume=true;
    }

    private void checkPermissions() {

        for (String p:permissions){
            // 首先是判断
            if (ContextCompat.checkSelfPermission(this, p) != PackageManager.PERMISSION_GRANTED) {
                ULog.i(TAG,"权限"+p+"没有授权成功1");

                // Should we show an explanation?
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, p)) {
                    // 显示给用户的解释
                    ULog.i(TAG,"权限"+p+"没有授权成功2--shouldShowRequestPermissionRationale");
                    ActivityCompat.requestPermissions(this, new String[]{p}, 1);
                } else {
                    // No explanation needed, we can request the permission.
                    ActivityCompat.requestPermissions(this, new String[]{p}, 1);
                    ULog.i(TAG,"权限"+p+"没有授权成功3--requestPermissions--"+p);

                }
            }else {
                ULog.i(TAG,"权限"+p+"-----授权成功");

            }
        }


    }

    @Override
    protected void onPause() {
        ULog.i(TAG, "-----onPause----" + this.getLocalClassName());
        super.onPause();
    }

    @Override
    protected void onStop() {
        ULog.i(TAG, "-----onStop----" + this.getLocalClassName());
        super.onStop();
        isActivityResume=false;

        boolean allResume = checkAllActivitysResume();
        if (!allResume){
            isAllActivityNoResume();
        }
    }

    //只要有一个界面可见就返回true
    protected boolean checkAllActivitysResume() {

        List<Activity> activities = ActivityManagers.getInstance().getActList();
        if (activities==null||activities.size()==0){
            isAllActivityNoResume();
            return false;
        }

        for(Activity act:activities){
            if (act instanceof BaseActivity){
                if (((BaseActivity)act).isActivityResume){
                    return true;
                }

            }
        }

        return false;





    }





    @Override
    protected void onRestart() {
        ULog.i(TAG, "-----onRestart----" + this.getLocalClassName());
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        ULog.i(TAG, "-----onDestroy----" + this.getLocalClassName());
        super.onDestroy();

        NetUtils.getInstance().cancelTag(this);
    }




    /**根据资源获取字符串*/
    protected String getStr(int res){
        return ApplicationHelper.mApplicationContecx.getResources().getString(res);
    }

    protected void startActivity(Class<?> cls){
        Intent intent=new Intent(this,cls);
        startActivity(intent);
    }



    protected void isAllActivityNoResume(){

    };
}
